package com.xxl.job.executor.service;

/**
 * 电话信号处理
 *
 * @author wang.kunlin
 * @date 2020年09月30日 9:39
 */

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.kafka.KafkaProduceService;
import com.xxl.job.executor.model.CurrentInfoDhModel;
import com.xxl.job.executor.model.CurrentInfoHkModel;
import com.xxl.job.executor.model.DevInfosModel;
import com.xxl.job.executor.model.SignalDefDhModel;
import com.xxl.job.executor.repository.CurrentInfoRepository;
import com.xxl.job.executor.utils.ThreadLocalDateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;


/**
 * 文件描述
 *
 * @author wang.kunlin
 * @date 2020年09月15日 16:56
 */
@Service
public class DhService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DhService.class);
    @Autowired
    private SignalDefService signalDefService;
    @Autowired
    private CurrentInfoRepository currentInfoRepository;
    @Autowired
    private KafkaProduceService kafkaProduceService;

    @Autowired
    private BkJsonDataService bkJsonDataService;

    /**
     *
     * @author wang.kunlin
     * @date 2020年0819日 16:56
     */

    public void save(SignalDefDhModel automaticControlModel, int flag, ArrayList<DevInfosModel> dataDevModelArrayList){
        SignalDefDhModel signalDefDhModel=signalDefService.queryDhSignal(
                automaticControlModel.getGatherId(), automaticControlModel.getGatherResId(),automaticControlModel.getSignalType(),automaticControlModel.getPointAddr());
        if(signalDefDhModel==null){
            // LOGGER.error("自控系统：未找到对应的信号配置");
            return;
        }
        String nDate="";
        nDate= ThreadLocalDateUtil.formatDate(new Date());

        String signalType  =signalDefDhModel.getSignalType();

        //新建实时存储的实体
        CurrentInfoDhModel currentInfoDhModel=new CurrentInfoDhModel();

        currentInfoDhModel.setValue(automaticControlModel.getSignalValue());
        currentInfoDhModel.setGatherResId(automaticControlModel.getGatherResId());
        currentInfoDhModel.setGatherId(automaticControlModel.getGatherId());
        currentInfoDhModel.setDevCode(signalDefDhModel.getDevCode());
        currentInfoDhModel.setSignalCode(signalDefDhModel.getCode());
        currentInfoDhModel.setSignalName(signalDefDhModel.getName());
        currentInfoDhModel.setSignalType(signalDefDhModel.getSignalType());
        currentInfoDhModel.setValunit(signalDefDhModel.getValunit());
        currentInfoDhModel.setTime(nDate);
        currentInfoDhModel.setPointAddr(signalDefDhModel.getPointAddr());

        //根据实体去redis查询上次信号时间
        CurrentInfoDhModel currentInfoDhModel1=currentInfoRepository.queryDhCurrent(signalDefDhModel.getDevCode(),signalDefDhModel.getCode());

        if(currentInfoDhModel1==null){
            currentInfoDhModel.setLastTime(nDate);
        }else{
            currentInfoDhModel.setLastTime(currentInfoDhModel1.getTime());
        }
        currentInfoDhModel.setSendTime(ThreadLocalDateUtil.formatDate(new Date()));
        boolean isSend=this.isSaveRedis(currentInfoDhModel,currentInfoDhModel1,signalType);
        if(isSend){
            //实时数据存入redis
            currentInfoRepository.save(currentInfoDhModel);

            //发送kafka
            sends(currentInfoDhModel);


//            //发送博坤
//            if(flag==1){
                DevInfosModel devInfosModel = bkJsonDataService.getDataRealTimeMonitorModel( signalDefDhModel.getCode(),
                        signalDefDhModel.getName(),
                        signalDefDhModel.getSignalType(),
                        signalDefDhModel.getDevCode(),
                        automaticControlModel.getSignalValue(),
                        signalDefDhModel.getValunit(),
                        nDate);
                dataDevModelArrayList.add(devInfosModel);
                bkJsonDataService.sendDevInfoMessage(dataDevModelArrayList);
//            }else{
//                DevInfosModel devInfosModel = bkJsonDataService.getDataRealTimeMonitorModel( signalDefDhModel.getCode(),
//                        signalDefDhModel.getName(),
//                        signalDefDhModel.getSignalType(),
//                        signalDefDhModel.getDevCode(),
//                        automaticControlModel.getSignalValue(),
//                        signalDefDhModel.getValunit(),
//                        nDate);
//                dataDevModelArrayList.add(devInfosModel);
//            }

           /* //发送博坤
            bkJsonDataService.sendDevInfoMessage(signalDefDhModel.getCode(),
                    signalDefDhModel.getName(),
                    signalDefDhModel.getSignalType(),
                    signalDefDhModel.getDevCode(),
                    automaticControlModel.getSignalValue(),
                    signalDefDhModel.getValunit(),
                    nDate);*/
        }
        //发送博坤
        if(flag==1 && dataDevModelArrayList.size()>0){
            bkJsonDataService.sendDevInfoMessage(dataDevModelArrayList);
        }
    }


    /**
     * 发kafka
     * @param message
     */
    public void sends(CurrentInfoDhModel message) {

        try {
            this.kafkaProduceService.sendDhAlarmMessage(message);

        } catch (Exception e) {
            XxlJobLogger.log("发送告警信息失败:"+message.toString(), e);
        }
    }



    /**
     * 判断实时数据是否需要存入redis
     * @param automaticControlStatus
     * @param automaticControlStatus1
     * @return
     */
    private boolean isSaveRedis(CurrentInfoDhModel automaticControlStatus,
                                CurrentInfoDhModel automaticControlStatus1, String signalType){
        int fa = 0;
        long iTimeDiffHour=0;
        String autoVal=automaticControlStatus.getValue();
        String oldautoVal=automaticControlStatus1.getValue();
        if(automaticControlStatus1 != null){
            if(!autoVal.equals(oldautoVal)){
                fa = 1;
            }
            iTimeDiffHour = (ThreadLocalDateUtil.parse(automaticControlStatus.getTime()).getTime() - ThreadLocalDateUtil.parse(automaticControlStatus1.getTime()).getTime()) / (1000 * 60* 20);
        }else{
            iTimeDiffHour = 2;
        }

        if ( fa > 0 || iTimeDiffHour > 1) {
            return true;
        }
        return false;
    }


}


